#include "thread.h"


int changeDir(dirID_t*dirID ,int netfd){
    train_t train;
    train_t rectrain;
    bzero(&train,sizeof(train_t));
    bzero(&rectrain,sizeof(train_t));

    recv(netfd,&rectrain.length,sizeof(int),MSG_WAITALL);
    recv(netfd,rectrain.buf,rectrain.length,MSG_WAITALL);
    char dirName[256];
    char path[256];
    bzero(dirName,256);
    bzero(path,256);
    strcpy(dirName,dirID->pwdID);
    printf("pwd_dirName =%s\n",dirName);
    strcpy(path,dirID->pwdID);
    if(strcmp(rectrain.buf,"..")==0||strcmp(rectrain.buf,"../")==0){
        if(dirID->userID.size>1){
            outDirQueue(&dirID->userID);
        }
        get_user_dir(train.buf,dirID);
        printf(".._buf = %s\n",train.buf);
    }
    else if(strcmp(rectrain.buf,"~")==0||strcmp(rectrain.buf,"~/")==0){
        while(dirID->userID.size>1){
            dirID->userID.pFornt=dirID->userID.pRear;
            dirID->userID.size=1;
            
        }
        get_user_dir(train.buf,dirID);
        printf("~_buf = %s\n",train.buf);
    }
    else if(strcmp(rectrain.buf,".")==0){
        get_user_dir(train.buf,dirID);
        printf("._buf = %s \n",train.buf);
    }
    else{

        char *name=strtok(rectrain.buf,"/");

        get_user_dir(train.buf,dirID);
        
        strcat(dirName,train.buf);
        printf("40_dirName = %s\n",dirName);
        int ret=access(dirName,F_OK);
        if(ret==-1){
            train.length=strlen(train.buf);
            send(netfd,&train.length,sizeof(int),MSG_NOSIGNAL);

            send(netfd,train.buf,train.length,MSG_NOSIGNAL);
            return 0;
            printf("erro\n");

        }
        strcpy(dirName,dirID->pwdID);
        
        char testName[256];
        if(strcmp(name,"~")==0) {

            while(dirID->userID.size>1){
                outDirQueue(&dirID->userID);
            }
            strcpy(testName,train.buf);
        }
        else if(strcmp(name,"..")==0){
            if(dirID->userID.size==1){
                
                train.length=strlen(train.buf);
                send(netfd,&train.length,sizeof(int),MSG_NOSIGNAL);

                send(netfd,train.buf,train.length,MSG_NOSIGNAL);
                return 0;
            }
            strcpy(testName,dirID->userID.pFornt->dirName);
            outDirQueue(&dirID->userID);

        }
        enDirQueue(&dirID->userID,name);
        strcat(dirName,"/");
        strcat(dirName,name);
        
        while(1){
            name=strtok(NULL,"/");
            if(!name){
                break;
            }
            strcat(dirName,"/");
            strcat(dirName,name);
            ret=access(dirName,F_OK);
            if(ret==-1){

                recDir(testName,&dirID->userID);
                train.length=strlen(train.buf);

                send(netfd,&train.length,sizeof(int),MSG_NOSIGNAL);
                send(netfd,train.buf,train.length,MSG_NOSIGNAL);
                return 0;

            }
            enDirQueue(&dirID->userID,name);
        }
        bzero(train.buf,sizeof(train.buf));
        get_user_dir(train.buf,dirID);
        printf("now buf = %s\n",train.buf);
        
    }
    printf("once over\n");
    train.length=strlen(train.buf);
    send(netfd,&train.length,sizeof(int),MSG_NOSIGNAL);

    printf("cd buf = %s\n",train.buf);
    send(netfd,train.buf,train.length,MSG_NOSIGNAL);
    return 0;

}
